package com.jn.glide.views;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.ListView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;
import com.jn.glide.R;
import com.jn.glide.adapter.AvatarsAdapter;
import com.jn.glide.adapter.NamesAdapter;
import com.jn.glide.service.ApiService;
import com.jn.glide.service.AvatarResponse;
import com.jn.glide.service.RetrofitClient;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private AvatarsAdapter avatarsAdapter;
    private List<AvatarResponse> avatars = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        ApiService apiService = RetrofitClient.getClient("https://api.oioweb.cn/").create(ApiService.class);
        Call<List<AvatarResponse>> call = apiService.getAvatars();
        call.enqueue(new Callback<List<AvatarResponse>>() {
            @Override
            public void onResponse(Call<List<AvatarResponse>> call, Response<List<AvatarResponse>> response) {
                if (response.isSuccessful()) {
                    avatars.clear();
                    avatars.addAll(response.body());
                    avatarsAdapter = new AvatarsAdapter(avatars, MainActivity.this);
                    recyclerView.setAdapter(avatarsAdapter);
                }
            }

            @Override
            public void onFailure(Call<List<AvatarResponse>> call, Throwable t) {
                Toast.makeText(MainActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void saveImageToGallery(Context context, String url) {
        Glide.with(context)
                .asBitmap()
                .load(url)
                .into(new CustomTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                        try {
                            String savedImagePath = MediaStore.Images.Media.insertImage(context.getContentResolver(), resource, "Title", "Description");
                            if (savedImagePath != null) {
                                Toast.makeText(context, "Image saved successfully", Toast.LENGTH_SHORT).show();
                            } else {
                                Toast.makeText(context, "Failed to save image", Toast.LENGTH_SHORT).show();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            Toast.makeText(context, "Failed to save image", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onLoadCleared(@Nullable Drawable placeholder) {
                        // 这里可以处理资源清除的逻辑
                    }
                });
    }
}